package com.example.myfirstapp;

import java.util.Calendar;
import java.util.TimeZone;
import java.util.Timer;
import java.util.TimerTask;

import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.util.Log;
import android.view.Menu;
import android.view.Window;
import android.widget.AnalogClock;
import android.widget.EditText;
import android.widget.TimePicker;

import com.services.weather.*;

public class MainActivity extends Activity {

	SharedPreferences.Editor editor;
	TimePicker minTimePicker;
	EditText currentAlarm;
	int hourOfDay;
	int minute;
    @SuppressWarnings("unchecked")
	@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        
        setContentView(R.layout.activity_main);
        
        minTimePicker = (TimePicker) findViewById(R.id.minTime);
        
        SharedPreferences sharedPref = this.getSharedPreferences(getString(R.string.prefFileName), Context.MODE_PRIVATE);
        
        editor = sharedPref.edit();
        
        currentAlarm = (EditText) findViewById(R.id.curAlarmText);
               
    }
    protected void onStart() {
    	super.onStart();
    	 
    	 minTimePicker.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {
    	 
             public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
            	 
             	String time = "";
             	time = hourOfDay + "," + minute;
       
                 	Log.d("SystemAlarm", "Earliest time to save to prefs: " + time);
                 	editor.putString(getString(R.string.saved_mintime), time);
                     //editor.commit();
                     editor.putString(getString(R.string.saved_curtime), time);
                     //editor.commit();
                     editor.putBoolean(getString(R.string.saved_hasSnooze), false);
                     //editor.commit();
                     Calendar curCal = setSystemAlarm(hourOfDay, minute);	
                  	editor.commit();

                 	 currentAlarm.setText(curCal.getTime().toString());
                 	 


             }
         });
         
    }
    
    protected void onPause() {
    	super.onPause();

    }
    
    protected void onStop() {
    	super.onStop();
    	finish();
    }

    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    private Calendar setSystemAlarm(int hour, int minute) {
    	//Create an offset from the current time in which the alarm will go off.
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.HOUR_OF_DAY, hour);
        cal.set(Calendar.MINUTE, minute);
        Log.d("SystemAlarm", "Alarm set to: " + cal.getTime().toString());
        //cal.add(Calendar.SECOND, 10);
        //Create a new PendingIntent and add it to the AlarmManager
        Intent intent = new Intent(this, AlarmReceiverActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this,
            12345, intent, PendingIntent.FLAG_CANCEL_CURRENT);
        AlarmManager am = 
            (AlarmManager)getSystemService(Activity.ALARM_SERVICE);
        am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),
                pendingIntent);
        
        return cal;
    }
     
}
